﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Infragistics.Win;
using Infragistics.Win.UltraWinGrid;
using Infragistics.Excel;
using Dashboard.ProductBonus.Business;

namespace Dashboard.ProductBonus
{
    public partial class FormDailyBonusList : Form
    {
        public FormDailyBonusList()
        {
            InitializeComponent();
        }

        private void ultraGridMain_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            // 自适应列宽
            e.Layout.AutoFitStyle = Infragistics.Win.UltraWinGrid.AutoFitStyle.ResizeAllColumns;
            // 是否允许调整列宽
            e.Layout.Override.AllowColSizing = AllowColSizing.Free;
            // 是否允许移动栏位顺序
            e.Layout.Override.AllowColMoving = AllowColMoving.NotAllowed;

            // 设置Grid的Split窗口个数，建议设置为1--不允许Split
            e.Layout.MaxColScrollRegions = 1;
            e.Layout.MaxRowScrollRegions = 1;

            // 冻结列
            e.Layout.UseFixedHeaders = false;

            // 排序
            e.Layout.Override.HeaderClickAction = Infragistics.Win.UltraWinGrid.HeaderClickAction.SortSingle;

            // 不允许删除
            e.Layout.Override.AllowDelete = DefaultableBoolean.False;

            // 滚动提示
            e.Layout.Bands[0].ScrollTipField = "StaffNo";

            // 隐藏不需要显示的列
            //e.Layout.Bands[0].Columns["WorkingDays"].Hidden = true;

            for (int i = 0; i < e.Layout.Bands[0].Columns.Count; i++)
            {
                e.Layout.Bands[0].Columns[i].CellActivation = Activation.NoEdit;
            }
        }

        private void toolStripButtonQuery_Click(object sender, EventArgs e)
        {            
            var dateFrom = this.dateTimePickerFrom.Value.Date;
            var dateTo = this.dateTimePickerTo.Value.Date;

            if (dateTo.Date < dateFrom.Date)
            {
                MessageBox.Show(this, "Date to can not earlier than Date from.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.dateTimePickerTo.Focus();
                return;
            }

            if (this.comboBoxStaffNo.SelectedItem == null && this.comboBoxStaffNo.Text != "")
            {
                MessageBox.Show(this, "Please choose a valid staff.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.comboBoxStaffNo.Focus();
                return;
            }

            try
            {
                string staffNo = this.comboBoxStaffNo.Text.Split('-')[0];

                BonusReport br = new BonusReport();
                DataTable table = br.GetDailyBonus(dateFrom, dateTo, this.comboBoxTeam.Text, staffNo, int.Parse(this.textBoxWorkingDays.Text));

                this.ultraGridMain.DataSource = table;
                this.ultraGridMain.DataBind();
                if (this.ultraGridMain.Rows.Count > 0)
                {
                    this.toolStripButtonExport.Enabled = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void toolStripButtonExport_Click(object sender, EventArgs e)
        {
            var dialogResult = this.saveFileDialogMain.ShowDialog();
            if (dialogResult == DialogResult.OK)
            {
                var fileName = this.saveFileDialogMain.FileName;
                this.ultraGridExcelExporterMain.Export(this.ultraGridMain, fileName);
                MessageBox.Show("Export data successfully!", "Information", MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
            }
        }

        private void toolStripButtonExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FormDailyBonusList_Load(object sender, EventArgs e)
        {
            this.BindTeamList();
            this.BindStaffNoList();
        }

        private void BindTeamList()
        {
            BusinessBase bb = new BusinessBase();
            DataTable teams = bb.LoadTeamList();

            this.comboBoxTeam.Items.Clear();
            this.comboBoxTeam.Items.Add("");

            foreach (DataRow row in teams.Rows)
            {
                this.comboBoxTeam.Items.Add(row["team"].ToString());
            }

            if (this.comboBoxTeam.Items.Count > 0)
            {
                this.comboBoxTeam.SelectedIndex = 0;
            }

            bb = null;
        }

        private void BindStaffNoList()
        {
            BusinessBase bb = new BusinessBase();
            DataTable staffs = bb.GetStaffOfTeam(this.comboBoxTeam.Text);

            this.comboBoxStaffNo.Items.Clear();
            this.comboBoxStaffNo.Items.Add("");

            foreach (DataRow row in staffs.Rows)
            {
                this.comboBoxStaffNo.Items.Add(row["staffno"].ToString() + "-" + row["name"].ToString());
            }
            if (this.comboBoxStaffNo.Items.Count > 0)
            {
                this.comboBoxStaffNo.SelectedIndex = 0;
            }
            bb = null;
        }

        private void comboBoxTeam_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.BindStaffNoList();
        }

        private void textBoxWorkingDays_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);
        }

        private void dateTimePickerFrom_ValueChanged(object sender, EventArgs e)
        {
            this.textBoxWorkingDays.Text = CalculateDays().ToString();
        }

        private void dateTimePickerTo_ValueChanged(object sender, EventArgs e)
        {
            this.textBoxWorkingDays.Text = CalculateDays().ToString();
        }


        private int CalculateDays()
        {
            DateTime dateFrom = dateTimePickerFrom.Value.Date;//初始化一个日期
            DateTime dateTo = dateTimePickerTo.Value.Date;//获取今天日期
            int countday = dateTo.Subtract(dateFrom).Days;//TimeSpan得到dt1和dt2的时间间隔
            //循环用来扣除总天数中的双休日
            int weekday = 0;
            if (countday >= 0)
            {
                DateTime tempdt = dateFrom;
                while (tempdt <= dateTo)
                {
                    if (tempdt.DayOfWeek != System.DayOfWeek.Saturday && tempdt.DayOfWeek != System.DayOfWeek.Sunday)
                    {
                        weekday++;
                    }
                    tempdt = tempdt.Date.AddDays(1);
                }
            }

            return weekday;
        }
    }
}
